#include <stdio.h>
#include <pwd.h>

/*
 * 获取用户登录相关信息
 *
 * struct passwd *getpwnam(const char *name);
 *
 * 若成功，返回指针；若出错或者达到文件尾端，返回NULL。
 */

#if 0
#include <pwd.h>
#include <sys/types.h>

/* 信息存贮在如下的结构体之中 */
struct passwd {
    char * pw_name; /* Username. */
    char * pw_passwd; /* Password. */
    __uid_t pw_uid; /* User ID. */
    __gid_t pw_gid; /* Group ID. */
    char * pw_gecos; /* Real name. */
    char * pw_dir; /* Home directory. -*/
    char * pw_shell; /* Shell program. */
};
#endif

int main()
{
    struct passwd * pw;
    char *username = "xiaozhi";

    pw = getpwnam(username);
    if (!pw) {
        printf("%s is not exist\n", username);
        return -1;
    }

    printf("pw->pw_name = %s\n", pw->pw_name);
    printf("pw->pw_passwd = %s\n", pw->pw_passwd);
    printf("pw->pw_uid = %d\n", pw->pw_uid);
    printf("pw->pw_gid = %d\n", pw->pw_gid);
    printf("pw->pw_gecos = %s\n", pw->pw_gecos);
    printf("pw->pw_dir = %s\n", pw->pw_dir);
    printf("pw->pw_shell = %s\n", pw->pw_shell);

    return 0;
}

#if 0
pw->pw_name = xiaozhi
pw->pw_passwd = x
pw->pw_uid = 1000
pw->pw_gid = 1000
pw->pw_gecos = ac,,,
pw->pw_dir = /home/xiaozhi
pw->pw_shell = /bin/bash
#endif
